package UIComponents// ActionScript file
{
	import fl.events.ComponentEvent;
	
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;

	public class PanelMain extends Sprite
	{
		public var qhWqPanel:UpOrDownWeaponPanel;
		private var _showIndex:int=0;
		private var nowShowingAry:Array=new Array();
		private var _nowVisibleId:int=-1;//当前拜访的好友id;
		private var visitleFilters:Array;
		private var mouseOverFilters:Array;
		
		private var _htxShow:Boolean=true;//显示红桃心

		public function PanelMain():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._showIndex=0;
			this.qhWqPanel=this.getChildByName("qhwq") as UpOrDownWeaponPanel;
			this.qhWqPanel.visible=true;
			(this.getChildByName("alertor") as Sprite).visible	= false;
			
			visitleFilters		=	 (this.getChildByName("visitleFilters") as FrameFriend).filters;
			mouseOverFilters	=	 (this.getChildByName("mouseOverFilters") as FrameFriend).filters;
			
			for(var i:uint=0; i<6; i++)
			{
				getFriendFrame(i).mouseOverFilters	=	mouseOverFilters;
				getFriendFrame(i).visitleFilters	=	visitleFilters;
			}
			
			this.addLister();
		}
		
		
		private function addLister():void
		{
			(this.getChildByName("button_turnLeft") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftHandler);
			(this.getChildByName("button_turnLeftLeft") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftLeftHandler);
			(this.getChildByName("button_turnRight") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightHandler);
			(this.getChildByName("button_turnRightRight") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightRightHandler);
		}
		
		
		/**
		 *设置左，右翻页按扭是否可见 
		 * @param showLeft	boolean		true为左可见
		 * @param showRight boolean		true为右可见
		 * 
		 */
		private function setButtonVisible():void
		{
			var filter:ColorMatrixFilter = new ColorMatrixFilter([0.3,0.6,0,0,0,0.3,0.6,0,0,0,0.3,0.6,0,0,0,0,0,0,1,0]) ;
			
			var showLeft:Boolean		=		this._showIndex>0;
			var showRight:Boolean		=		this.nowShowingAry.length>6		&&	this.showIndex < this.nowShowingAry.length-6;
			
			if(showLeft){
				(this.getChildByName("button_turnLeft") as MyButton).mouseChildren			=		true;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseChildren		=		true;
				(this.getChildByName("button_turnLeft") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseEnabled		=		true;				
				(this.getChildByName("button_turnLeft") as MyButton).filters					=		null;
				(this.getChildByName("button_turnLeftLeft") as MyButton).filters				=		null;
			}
			else
			{
				(this.getChildByName("button_turnLeft") as MyButton).mouseChildren			=		false;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseChildren		=		false;
				(this.getChildByName("button_turnLeft") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseEnabled		=		false;					
				(this.getChildByName("button_turnLeft") as MyButton).filters					=		[filter];
				(this.getChildByName("button_turnLeftLeft") as MyButton).filters				=		[filter];
			}
			
			if(showRight){
				(this.getChildByName("button_turnRight") as MyButton).mouseChildren			=		true;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseChildren	=		true;
				(this.getChildByName("button_turnRight") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseEnabled		=		true;				
				(this.getChildByName("button_turnRight") as MyButton).filters				=		null;
				(this.getChildByName("button_turnRightRight") as MyButton).filters			=		null;
			}
			else
			{
				(this.getChildByName("button_turnRight") as MyButton).mouseChildren			=		false;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseChildren	=		false;
				(this.getChildByName("button_turnRight") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseEnabled		=		false;					
				(this.getChildByName("button_turnRight") as MyButton).filters				=		[filter];
				(this.getChildByName("button_turnRightRight") as MyButton).filters			=		[filter];
			}
		}
		private function turnLeftHandler(e:ComponentEvent):void
		{
			if(this.showIndex==0)return;
			this._showIndex-=1;
			this.reshow();
		}
		
		private function turnLeftLeftHandler(e:ComponentEvent):void
		{
			if(this.showIndex<6)
			{
				this._showIndex=0;
			}
			else
			{
				this._showIndex-=6;
			}
			this.reshow();
		}
		
		private function turnRightHandler(e:ComponentEvent):void
		{
			if(null==this.nowShowingAry || this.nowShowingAry.length<1) return;
			if(this.showIndex==this.nowShowingAry.length-6)return;
			this._showIndex+=1;
			this.reshow();
		}
		
		private function turnRightRightHandler(e:ComponentEvent):void
		{
			if(null==this.nowShowingAry || this.nowShowingAry.length<1) return;
			if(this.showIndex>this.nowShowingAry.length-12)
			{
				this._showIndex=this.nowShowingAry.length-6;
			}
			else
			{
				this._showIndex+=6;
			}
			this.reshow();
		}
		
		
		public function getFriendFrame(index:int):FrameFriend
		{
			if(index<0 || index>5)throw new Error("索引出错");
			return (this.getChildByName("frameFriend_"+(index+1))) as FrameFriend;
		}
		
		
		public function reshow():void
		{
			if(null==this.nowShowingAry && this.nowShowingAry.length<1)return;
			for(var i:int=0;i<6;i++)
			{
				var friendFrame:FrameFriend=this.getFriendFrame(i);
				friendFrame.reset();
				friendFrame._bmb.visible=true;
				var ifriend:IFriendInfo=this.nowShowingAry[this.showIndex+i] as IFriendInfo;
				
				
				friendFrame.htx_icon.visible	=	false;
				friendFrame.isVisitle	=	false;			
				if(null!=ifriend)
				{
					friendFrame.setFriendData(ifriend);

					if( ifriend.friendID	==	this._nowVisibleId){		//是当前所拜访的好友
						friendFrame.isVisitle	=	true;
					}
					
					if(!ifriend.isMySelft)
					if( (ifriend.visitSign && ifriend.friendAct>0) || (ifriend.visitSign==0 && this.visitleNum<maxVisitleNum))friendFrame.htx_icon.visible	 =	true;
				}
			}
			
			setButtonVisible();
		}
		
		
		public var maxVisitleNum:Number=-1;
		/**
		 *已经拜访的好友数 
		 * @return 
		 * 
		 */
		public function get visitleNum():Number
		{
			var vn:Number=0;
			for( var i:uint=0; i<this.nowShowingAry.length; i++)
			{
				var ifriend:IFriendInfo=this.nowShowingAry[i] as IFriendInfo;
				if(ifriend.visitSign==1 && !ifriend.isMySelft)vn++;
			}
			return vn;
		}
		/**
		 *设置当前所拜访的好友id 
		 * @param value
		 * 
		 */
		public function setVisibleId(value:uint):void
		{
			this._nowVisibleId		=	value;
			reshow();
		}
		
		public function setFriendArr(arr:Array):void
		{
			this.nowShowingAry=arr;
			reshow();
		}

		public function get showIndex():int
		{
			return _showIndex;
		}

		public function get htxShow():Boolean
		{
			return _htxShow;
		}
		/**
		 *设置是否显示红桃。 
		 * @param v
		 * 
		 */
		public function set htxShow(v:Boolean):void
		{
			this._htxShow	=	v;
		}
		
		
	}
}